// apis/uni-fetch.js

// 导入安装好的 uni-app-fetch 模块
import { createUniFetch } from 'uni-app-fetch';
import store from '@/store';
import { getToken, removeToken } from '@/utils/auth';

// 定义 tabBar 的路径
const tabBarPagePaths = [
	'pages/index/index',
	// 'pages/message/index',
	'pages/my/index'
];
// const baseURL = 'http://192.168.3.37:8808/api/erp'

// const imgBaseUrl = 'http://192.168.3.37:8808/static'
const imgBaseUrl = 'http://oss.yukukeji.com/';
const baseURL = 'https://test.yukukeji.com/yuku/api'
// const baseURL = 'https://www.yukukeji.com/yuku/api'
// const imgBaseUrl = 'https://www.yukukeji.com/static'

// const baseURL = 'http://192.168.3.23:8808/api/erp'
// const baseURL = '/miniApp';
// const imgBaseUrl = 'http://192.168.3.17/static'

// 配置符合自身业务的请求对象
let num = 0;
export const uniFetch = createUniFetch({
	loading: {
		title: '正在加载...',
		mask: true
	},
	baseURL,
	intercept: {
		// 请求拦截器
		request(options) {
			num++;
			if (store.getters.token) {
				const defaultHeaders = {
					token: getToken()
				};
				options.header = Object.assign({}, defaultHeaders, options.header);
			}
			console.log('11111111');
			return options;
		},
		// 响应拦截器
		response({ data }) {
			if (data.code === 401) {
				num--;
				console.log('401');
				if (num >= 0) {
					num = 0;
					removeToken();
					// 页面栈
					const pageStack = getCurrentPages();
					let query = location ? location.search.substring(1) : '';
					console.log(query, 'query');
					const storeId = query ? query.split('=')[1] : '';
					// 当前页面地址
					const redirectURL = pageStack[pageStack.length - 1]?.route ? pageStack[pageStack.length - 1].route : '';
					console.log('redirectURL', redirectURL);
					// tabBar 页面需要使用 switchTab 跳转
					const routeType = tabBarPagePaths.includes(redirectURL) ? 'switchTab' : 'redirectTo';

					return uni.redirectTo({
						url: `/pages/login/index?storeId=${storeId}&routeType=${routeType}&redirectURL=/${redirectURL}`
					});
				}
			}
			return data;
		}
	}
});

export const fileUpload = filePath => {
	// console.log(URL.createObjectURL(filePath),'upload')
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '图片处理中',
			mask: true
		});
		console.log(filePath, 'filePath');
		console.log(getToken(), 'toekn');
		uni.uploadFile({
			url: baseURL + '/miniApp/file/uploadFile',
			filePath,
			header: {
				// 'Content-Type': 'multipart/form-data',
				token: getToken()
			},
			formData: {
				// 该字段是后端除了需要文件之外的其他参数
				fileType: 'image/jpg'
			},
			// name的值为后端接口与文件相对于的字段名
			name: 'multipartFile',
			success(res) {
				console.log(res, 'res0000000');
				if (res.statusCode == 200) {
					const fileObj = JSON.parse(res.data).data;
					let onlineFilePath = imgBaseUrl + fileObj.filePath + fileObj.fileNewName;
					console.log(onlineFilePath, 'onlineFilePath');
					resolve(onlineFilePath);
					uni.hideLoading();
				} else {
					uni.hideLoading();
					uni.utils.toast('图片上传失败');
					reject(res);
				}
			},
			fail(f) {
				uni.hideLoading();
				console.log('失败', f);
			}
		});
	});
};
